
<?php

header("content-type:text/html;charset=utf-8");

class UserController extends Controller {

    public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
		);
	}
    public function actions() {
        return array(
            'captcha' => array(
                'class' => 'CCaptchaAction',
                'backColor'=>'0xFFFFFF',  //背景颜色
                'minLength'=>4,  //最短为4位
                'maxLength'=>4,   //是长为4位
                 'transparent' => true, //显示为透明，当关闭该选项，才显示背景颜色
            ),
        );
    }
public function accessRules(){
	return array(
		array('allow',
            'actions'=>array('captcha'),
            'users'=>array('*'),
            ),
            array('allow',  // deny all users
				'users'=>array('*'),
                ),
	);
}
    public function actionLogin() {
        $login = new LoginForm();
        if (isset($_POST['LoginForm'])) {
            $login->attributes = $_POST['LoginForm'];
            if ($login->validate() && $login->login()) {
                $this->redirect('./index.php?r=user/view');
            }
        }
        $this->render('login', array('login' => $login));
    }

    public function actionLogout() {
        Yii::app()->session->clear(); //删除内存中的session变量
        Yii::app()->session->destroy(); //删除服务器session文件
        //Yii::app()->user->logout();
        $this->redirect('./index.php?r=user/view');
    }

    public function actionIndex() {
        $this->render('index');
    }

    // Uncomment the following methods and override them if needed
    /*
      public function filters()
      {
      // return the filter configuration for this controller, e.g.:
      return array(
      'inlineFilterName',
      array(
      'class'=>'path.to.FilterClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }

      public function actions()
      {
      // return external action classes, e.g.:
      return array(
      'action1'=>'path.to.ActionClass',
      'action2'=>array(
      'class'=>'path.to.AnotherActionClass',
      'propertyName'=>'propertyValue',
      ),
      );
      }
     */
    public function actionView() {
        $user = UserTable::model()->findAll();
        $this->render('view', array('user' => $user));
    }

    public function actionViewUser($id) {
        $user = UserTable::model()->findByPk($id);
        echo 'Username:' . $user->user_name . '<br/>Nickname:' . $user->nick_name . '<br/>Password:' . $user->password . '<br/>Sex:' . $user->sex . '<br/>Education Background:'
        . $user->edubg . '<br/>Hobby:' . $user->hobby . '<br/>User email:  ' . $user->user_email . '<br/>Phone Number:' . $user->user_phone_num;
    }

    public function actionAdd() {
        $newUsr = new UserTable;
        $sex[1] = "男";
        $sex[2] = "女";

        $edubg[1] = "--请选择--";
        $edubg[2] = "小学";
        $edubg[3] = "中学";
        $edubg[4] = "大学";

        $hobby[1] = "篮球";
        $hobby[2] = "足球";
        $hobby[3] = "羽毛球";
        $hobby[4] = "乒乓球";
        if (isset($_POST['UserTable'])) {
            //$newUsr->user_name=$_POST['UserTable']['user_name'];
            //$newUsr->nick_name=$_POST['UserTable']['nick_name'];
            //$newUsr->password=$_POST['UserTable']['password'];
//			foreach($_POST['UserTable'] as $_k => $_v)
//			{
//			$newUsr->$_k=$_v;
//                        }
            if (is_array($_POST['UserTable']['hobby'])) {
                $_POST['UserTable']['hobby'] = implode(',', $_POST['UserTable']['hobby']);
            }
            $_POST['UserTable']['password'] = md5($_POST['UserTable']['password']);
            $_POST['UserTable']['password2'] = md5($_POST['UserTable']['password2']);
            $newUsr->attributes = $_POST['UserTable'];

            if ($newUsr->save()) {
                $this->redirect('./index.php?r=user/view');
            }
        }
        $this->render('addPage', array('newUser' => $newUsr, 'sex' => $sex, 'edubg' => $edubg, 'hobby' => $hobby));
    }

    /* 	public function actionAddUser(){
      $newUser=new UserTable;
      $newUser->user_name=$_POST['user_name'];
      $newUser->nick_name=$_POST['nick_name'];
      $newUser->password=$_POST['password'];
      $newUser->save();
      //	print_r($_POST['UserTable']);

      $user=UserTable::model()->findAll();
      foreach($user as $oneuser)
      echo '<a text-decoration;none;href=index.php?r=user/viewUser&id='.$oneuser->id.'>'.$oneuser->user_name.'&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
      <a href=index.php?r=user/edit&id='.$oneuser->id.'><button>edit</button></a>&nbsp&nbsp
      <a href=index.php?r=user/delete&id='.$oneuser->id.'><button>delete</button></a><br/>';

      } */

    public function actionUpdate($id) {
        $sex[1] = "男";
        $sex[2] = "女";
        $edubg[1] = "--请选择--";
        $edubg[2] = "小学";
        $edubg[3] = "中学";
        $edubg[4] = "大学";
        $nowUser = UserTable::model()->findByPk($id);
        //$nowUser=UserTable::model()->findByPk($_POST['id']);
        if (isset($_POST['UserTable'])) {
            foreach ($_POST['UserTable'] as $_k => $_v) {
                $nowUser->$_k = $_v;
            }
            //	$nowUser->user_name=$_POST['user_name'];
            //	$nowUser->nick_name=$_POST['nick_name'];
            //	$nowUser->password=$_POST['password'];
            if ($nowUser->save())
                $this->redirect('./index.php?r=user/view');
        }
        $this->render('update', array('nowUser' => $nowUser, 'sex' => $sex, 'edubg' => $edubg));
    }

    public function actionDelete($id) {
        $user = UserTable::model()->findByPk($id);
        $user->delete();
        $this->redirect('./index.php?r=user/view');
    }

}
